/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.badlogicgames.superjumper;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;

public class CopyWorldRenderer {
	static final float FRUSTUM_WIDTH = Settings.FRUSTUM_WIDTH;
	static final float FRUSTUM_HEIGHT = Settings.FRUSTUM_HEIGHT;
	World world;
	OrthographicCamera cam;
	SpriteBatch batch;
	TextureRegion background;
	float barImageBarFullSize = 5;
	
	
	Vector2 healthBarBackgroundLocation;
	
	Vector2 staminaBarBackgroundLocation;
	//Vector2 healthBarLocation = new Vector2();
	//Vector2 staminaBarLocation =new Vector2();

	public CopyWorldRenderer (SpriteBatch batch, World world) {
		this.world = world;
		this.cam = new OrthographicCamera(FRUSTUM_WIDTH, FRUSTUM_HEIGHT);		
		this.cam.position.set(Settings.WORLD_WIDTH-FRUSTUM_WIDTH+  FRUSTUM_WIDTH / 2,FRUSTUM_HEIGHT / 2, 0);  // camera position
		this.batch = batch;
	}

	public void render () {
		// move the camera base on position of pigeon 
		if (world.bob.position.y > cam.position.y+2) cam.position.y = world.bob.position.y-2;
		if (world.bob.position.y < cam.position.y-7) cam.position.y = world.bob.position.y+7;
		if (world.bob.position.x > cam.position.x+4) cam.position.x = world.bob.position.x-4;
		if (world.bob.position.x < cam.position.x-4) cam.position.x = world.bob.position.x+4;
		cam.update();
		batch.setProjectionMatrix(cam.combined);
		renderBackground();
		renderObjects();
	}

	public void renderBackground () {
		batch.disableBlending();
		batch.begin();
		//batch.draw(textureregion,xpos,ypos,originx, originy, width, height, scalex, scaley, rotation);
		batch.draw(Assets.backgroundRegion, 16, -1, 0,0, Settings.WORLD_HEIGHT+5,Settings.WORLD_WIDTH+5, 1,1, 90);
		
		
		healthBarBackgroundLocation = new Vector2((float)(cam.position.x - FRUSTUM_WIDTH / 2+1.0), 
				(float)(cam.position.y - FRUSTUM_HEIGHT / 2+1) 
				);
		
		staminaBarBackgroundLocation = new Vector2((float)(cam.position.x - FRUSTUM_WIDTH / 2+1.0), 
				(float)(cam.position.y - FRUSTUM_HEIGHT / 2+9) 
				);
		
		
		batch.draw(Assets.barBackground, 
				this.healthBarBackgroundLocation.x,this.healthBarBackgroundLocation.y, 
				0,0,
				(float)(barImageBarFullSize)
				,
				(float)0.51
				,1,1,90);
	
		
		
		batch.draw(Assets.barBackground,
				this.staminaBarBackgroundLocation.x, this.staminaBarBackgroundLocation.y,
				0,0,
				(float)(barImageBarFullSize)
				,
				(float)0.51
				,1,1,90);
		
		renderBars();
		batch.end();
	}
	

	public void renderObjects () {			
		batch.enableBlending();
		batch.begin();
		renderBob();
		renderTurrets();
		renderPlatforms();
		renderItems();
		renderSquirrels();
		renderCastle();
		batch.end();		
	}
	
	private void renderBars(){
		float healthPercent = Pigeon.health/Pigeon.MAX_HEALTH;
		float staminaPercent = Pigeon.stamina/Pigeon.MAX_STAMINA;
		Gdx.app.log("healthPercent", healthPercent+" ");
		Gdx.app.log("staminaPercent", staminaPercent+" ");
		batch.draw(Assets.healthBarUnit, 
				this.healthBarBackgroundLocation.x, 
				this.healthBarBackgroundLocation.y, 
				0,0,
				(float)(barImageBarFullSize * healthPercent),
				(float)0.50
				,1,1,90);
		
		batch.draw(Assets.staminaBarUnit, 
				this.staminaBarBackgroundLocation.x, 
				this.staminaBarBackgroundLocation.y, 
				0,0,
				(float)(barImageBarFullSize * staminaPercent),
				(float)0.50
				,1,1,90);
	}

	private void renderBob () {
		TextureRegion keyFrame;
		switch (world.bob.state) {
		case Pigeon.BOB_STATE_FALL:
			keyFrame = Assets.bobFall.getKeyFrame(world.bob.stateTime, Animation.ANIMATION_LOOPING);
			break;
		case Pigeon.BOB_STATE_NORMAL:
			keyFrame = Assets.bobJump.getKeyFrame(world.bob.stateTime, Animation.ANIMATION_LOOPING);
			break;
		case Pigeon.BOB_STATE_DIED:
		case Pigeon.BOB_STATE_HIT:
		default:
			keyFrame = Assets.bobHit;
		}

		float side = world.bob.velocity.y < 0 ? -1 : 1;
		Sprite s = new Sprite(keyFrame);
		s.rotate(90);
//		if(side < 0)
//			batch.draw(keyFrame, world.bob.position.x + 0.5f, world.bob.position.y - 0.5f,
//					side * 1, 1);
//		else
//			batch.draw(keyFrame, world.bob.position.x - 0.5f, world.bob.position.y - 0.5f, side * 1, 1);
	
		
		
//		if(side < 0)
//		batch.draw(keyFrame, world.bob.position.x + 0.5f, world.bob.position.y - 0.5f,
//				0,Pigeon.BOB_HEIGHT/2,
//				side * 1, 1
//				,1,1,90);
//	else
//		batch.draw(keyFrame, world.bob.position.x - 0.5f, world.bob.position.y - 0.5f,
//				Pigeon.BOB_WIDTH/2,Pigeon.BOB_HEIGHT/2,
//				side * 1, 1
//				,1,1,90);
		batch.draw(keyFrame, world.bob.position.x - 0.5f, world.bob.position.y - 0.5f,
				Pigeon.BOB_WIDTH/2,Pigeon.BOB_HEIGHT/2,
				 1, 1
				,1,1,90);
	
	}

	private void renderPlatforms () {
		int len = world.platforms.size();
		for (int i = 0; i < len; i++) {
			Platform platform = world.platforms.get(i);
			TextureRegion keyFrame = Assets.platform;
			if (platform.state == Platform.PLATFORM_STATE_PULVERIZING) {
				keyFrame = Assets.brakingPlatform.getKeyFrame(platform.stateTime, Animation.ANIMATION_NONLOOPING);
			}

			batch.draw(keyFrame, platform.position.x - 1, platform.position.y - 0.25f, 2, 0.5f);
		}
	}

	private void renderItems () {
		int len = world.springs.size();
		for (int i = 0; i < len; i++) {
			Spring spring = world.springs.get(i);
			batch.draw(Assets.spring, spring.position.x - 0.5f, spring.position.y - 0.5f, 1, 1);
		}

		len = world.coins.size();
		for (int i = 0; i < len; i++) {
			Coin coin = world.coins.get(i);
			TextureRegion keyFrame = Assets.coinAnim.getKeyFrame(coin.stateTime, Animation.ANIMATION_LOOPING);
			batch.draw(keyFrame, coin.position.x - 0.5f, coin.position.y - 0.5f, 1, 1);
		}
	}
	
	/**
	 * Renders Items like Health, Shield etc based on ItemType.
	 * */

	private void renderSquirrels () {
		int len = world.squirrels.size();
		for (int i = 0; i < len; i++) {
			Squirrel squirrel = world.squirrels.get(i);
			TextureRegion keyFrame = Assets.squirrelFly.getKeyFrame(squirrel.stateTime, Animation.ANIMATION_LOOPING);
			float side = squirrel.velocity.x < 0 ? -1 : 1;
			if(side < 0 )
				batch.draw(keyFrame, squirrel.position.x + 0.5f, squirrel.position.y - 0.5f, side * 1, 1);
			else
				batch.draw(keyFrame, squirrel.position.x - 0.5f, squirrel.position.y - 0.5f, side * 1, 1);
		}
	}

	private void renderCastle () {
		Castle castle = world.castle;
		batch.draw(Assets.castle, castle.position.x - 1, castle.position.y - 1, 2, 2);
	}
	
	private void renderTurrets(){
		int len = world.turrets.size();
		for(int i=0;i<len;i++){
			Turret t = world.turrets.get(i);
			batch.draw(Assets.turretNozzle, t.position.x, t.position.y, Turret.ORIGIN_X, Turret.ORIGIN_Y, Turret.WIDTH, Turret.HEIGHT, 1, 1, t.rotation, t.clockwise);	
			//batch.draw(Assets.turretBase, t.position.x+1, t.position.y-1, 0, 0, Turret.WIDTH, Turret.HEIGHT,1,1,90);
			batch.draw(Assets.turretBase,t.position.x+1.35f,t.position.y-0.75f, 0, 0, Turret.WIDTH, Turret.HEIGHT,1,1,90);
			renderBullets(t);
		}
		
	}
	
	private void renderBullets(Turret t){
		int numberOfBullets = t.bullets.size();
		for(int i=0;i<numberOfBullets;i++){
			Bullet b = t.bullets.get(i);
			TextureRegion keyFrame = Assets.bulletAnim.getKeyFrame(b.stateTime, Animation.ANIMATION_LOOPING);
			batch.draw(keyFrame, b.position.x, b.position.y,Bullet.BULLET_WIDTH, Bullet.BULLET_HEIGHT);
		}	
	}
}
